package com.example.springboot2.oa.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.springboot2.base.web.BaseController;
import com.example.springboot2.oa.entity.TStudent;
import com.example.springboot2.oa.entity.TUser;
import com.example.springboot2.oa.service.ITStudentService;
import com.example.springboot2.oa.service.ITUserService;
import com.example.springboot2.tools.MakeResponse;
import com.example.springboot2.tools.ResponseMessage;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  用户控制层
 * </p>
 *
 * @author llfy
 * @since 2018-11-23
 */
@CrossOrigin
@RestController
@RequestMapping("/oa/t-student")
public class TStudentController extends BaseController {

    /**
     *注入userService
     */
    @Autowired
    private ITStudentService studentService;
    @Autowired
    private ITUserService userService;

     /**
     * 用户列表
     * @return
     */
    @ApiOperation(value = "学生列表")
    @GetMapping("listStudent")
    public ResponseMessage listStudent(String token,String studentName){
        //验证是否登陆
        QueryWrapper<TUser> userToken = new QueryWrapper<>();
        userToken.eq("token",token);
        TUser tUser = userService.getOne(userToken);
        if (tUser == null){
            return MakeResponse.success("401","无权限访问",null);
        }
        List<TStudent> data = studentService.listStudent(studentName);
        return MakeResponse.success("200","用户列表",data);
    }

    /**
     * 新增用户
     * @param user
     * @return
     */
    @ApiOperation(value = "新增学生")
    @PostMapping("saveStudent")
    public ResponseMessage saveStudent(@RequestBody TStudent student,HttpServletRequest request){
        //验证名称是否重复
//        QueryWrapper<TUser> wrapper = new QueryWrapper<>();
//        wrapper.eq("user_name",user.getUserName());
//        TUser userFlag = userService.getOne(wrapper);
//        if (userFlag != null){
//            return MakeResponse.success("500","用户名已存在");
//        }
        //新增操作
        //user.setId(String.valueOf(UUID.randomUUID()));
        student.setCreateTime(new Date());
        //user.setUserPwd("000000");
        //user.setUserState(0);
        Boolean flag = studentService.save(student);
        if (flag){
            return MakeResponse.success("200","新增成功");
        }
        return MakeResponse.success("500","新增失败");
    }

    /**
     * 修改学生
     * @param user
     * @return
     */
    @ApiOperation(value = "修改学生")
    @PostMapping("updateStudent")
    public ResponseMessage updateUser(@RequestBody TStudent student,HttpServletRequest request){
        //验证是否登陆
        QueryWrapper<TUser> userToken = new QueryWrapper<>();
        userToken.eq("token",request.getHeader("token"));
        TUser tUser = userService.getOne(userToken);
        if (tUser == null){
            return MakeResponse.success("401","无权限访问");
        }
        //验证名称是否重复
//        QueryWrapper<TUser> wrapper = new QueryWrapper<>();
//        wrapper.eq("user_name",user.getUserName()).ne("id",user.getId());
//        TUser userFlag = userService.getOne(wrapper);
//        if (userFlag != null){
//            return MakeResponse.success("500","用户名已存在");
//        }
        //修改操作
        //student.setUpdateTime(new Date());
        Boolean flag = studentService.updateById(student);
        if (flag){
            return MakeResponse.success("200","更新成功");
        }
        return MakeResponse.success("500","更新失败");
    }

    /**
     * 删除学生
     * @param studentId
     * @return
     */
    @ApiOperation(value = "删除学生")
    @PostMapping("deleteStudent")
    public ResponseMessage deleteStudent(int studentId,HttpServletRequest request){
        //验证是否登陆
        QueryWrapper<TUser> userToken = new QueryWrapper<>();
        userToken.eq("token",request.getHeader("token"));
        TUser tUser = userService.getOne(userToken);
        if (tUser == null){
            return MakeResponse.success("401","无权限访问");
        }
        //删除操作
        Boolean flag = studentService.removeById(studentId);
        if (flag){
            return MakeResponse.success("200","删除成功");
        }
        return MakeResponse.success("500","删除失败");
    }
}
